package br.com.yurikimo.ykkit;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
//import android.widget.Toast;

public abstract class YKWebViewClient extends WebViewClient{
	
	private ProgressDialog progressDialog = null;

	public void onPageFinished(WebView view, String url){
		if (progressDialog != null){
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	public void onPageStarted(WebView view, String url, Bitmap favicon){
		if (progressDialog == null){
			progressDialog = ProgressDialog.show(YKDirector.getActivity(), null, "Loading...");
		}
	}

	public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
		onReceivedError(errorCode, description);
	}
	
	public abstract void onReceivedError(int errorCode, String description);
	
	public boolean shouldOverrideUrlLoading(WebView view, String url)
	{
		//Toast.makeText(YKDirector.getActivity(), url, Toast.LENGTH_SHORT).show();
		
		if (url.startsWith("vnd.youtube")){                    
			YKDirector.getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
		} 
		else if (url.contains("about:blank")){

		}
		else {
			view.loadUrl(url);
		}
		
		return true;
	}
}
